package com.mkyong.web.controller;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.bonc.xcloud.jdbc.XCloudResultSet;
import com.bonc.xcloud.jdbc.XCloudStatement;

@Controller
public class MainController2 {

	private Connection conn = null;

	private String driver = "com.bonc.xcloud.jdbc.XCloudDriver"; // 驱动

	private String url = "jdbc:xcloud:@172.16.87.114:1803/WCL"; // 连接字符串

	private String username = "wcl"; // 用户名

	private String password = "wcl123"; // 密码
	private XCloudStatement pstmt;

	@RequestMapping(value = "/start", method = RequestMethod.GET)
	public void getPages() throws SQLException {
		String sql = "select * from DM_DP_ETPR_CHEQUE_LIST";
		query(sql, false);
	}

	private void query(String sql, boolean b) throws SQLException {
		pstmt = (XCloudStatement) getConn().createStatement();
		// 建立一个结果集，用来保存查询出来的结果
		XCloudResultSet rs = (XCloudResultSet) pstmt.executeQuery(sql);
		while (rs.next()) {
			String name = rs.getString(1);
			System.out.println("1111111111111-------------" + name);
		}
		rs.close();
		pstmt.close();

	}

	private Connection getConn() {
		// TODO Auto-generated method stub
		if (conn == null) {
			try {
				Class.forName(driver);
				conn = DriverManager.getConnection(url, username, password);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return conn;
	}

	@RequestMapping(value = "/pause", method = RequestMethod.GET)
	public void pause()  {

		try {
			if (pstmt != null && !pstmt.isClosed()) {
				try {
					pstmt.cancel();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
		}

	}

}
